<?php
/**
 * 订单管理
 * Created by PhpStorm.
 * User: andy
 * Date: 2019/11/27
 * Time: 下午6:34
 */

namespace app\control\controller;

use app\common\model\CarBrandModel;
use app\common\model\CarModel;
use app\common\model\CarSeriesModel;
use app\common\model\CarServiceModel;
use app\common\model\MemberModel;
use app\common\model\OrderModel;
use think\facade\Request;

class OrderController extends AdminBaseController
{
    /**
     * 文章列表
     */
    public function index(OrderModel $order){
        $data=Request::param("");
        $where=$this->search($data);

        $list=$order->getALL($where);

        $svr=CarServiceModel::field('id,title')
            ->where('status','>',CarServiceModel::DEL_STATUS)
            ->select();

        $this->assign("svr",$svr);
        $this->assign("list",$list);
        $this->assign("gd",$data);
        return $this->fetch();
    }

    /**
     * 查看详情
     * @param $id
     * @param CarServiceModel $cs
     * @return mixed
     */
    public function detail($id,OrderModel $cs){
        $info=$cs::get($id);

        $brand=CarBrandModel::field('brand')->where('brand_id',$info->brand_id)->find();
        $series=CarSeriesModel::field('series_name')->where('series_id',$info->series_id)->find();
        $car=CarModel::field('car_name')->where('car_id',$info->car_id)->find();

        if(!empty($brand)) $info->brand_name=$brand->brand;
        if(!empty($series)) $info->series_name=$series->series_name;
        if(!empty($car)) $info->car_name=$car->car_name;

        $this->assign('info',$info);
        return $this->fetch();
    }

    /**
     * 搜索
     * @return array
     */
    private function search($data){

        $where=[];

        if(!empty($data["number"])){
            $where[]=["order_number","=",$data["number"]];
        }

        if(!empty($data["service_id"])){
            $where[]=["service_id","=",$data["service_id"]];
        }

        if (!empty($data["status"]) && $data["status"] != -2) {
            $where[] = ["status", '=', $data['status'] - 1];
        }

        if (!empty($data["type"]) && $data["type"] != -2) {
            $where[] = ["order_type", '=', $data['type'] - 1];
        }

        if (!empty($data["mobile"])) {
            $u = MemberModel::where('mobile', $data["mobile"])
                ->field('uid')
                ->find();

            $where[] = ["user_id", '=', $u['uid']];
        }

        return $where;
    }

    /**
     * 状态修改
     */
    public function acStatus(){
        $id=Request::post("id");
        $status=Request::post("status");

        $am=OrderModel::get($id);
        $am->order_type=$status;
        $am->finish_time=time();
        $st=$am->save();

        if($st){
            $this->success("操作成功！");
        }else{
            $this->error("操作失败！");
        }
    }


    /**
     * 删除
     */
    public function del(OrderModel $m)
    {
        $id = Request::post("id");
        $type = Request::post("type");

        if(empty($type)){
            $status = $m->del($id);
            if ($status['status']) {
                $this->success($status['msg']);
            } else {
                $this->error($status['msg']);
            }

        }else{
            $error = 0;
            $sussuc = 0;
            foreach ($id as $vo) {
                $status = $m->del($vo);
                if (!$status['status']) {
                    $error++;
                    continue;
                } else {
                    $sussuc++;
                }
            }

            $this->success("成功删除{$sussuc}条，删除失败{$error}条");
        }

    }

    public function _export(){
        $name='订单数据';
        $tit=[
            'ID',
            '订单用户',
            '订单编号',
            '服务项目',
            '汽车品牌',
            '汽车系列',
            '汽车型号',
            '联系人',
            '联系人电话',
            '陪同时间',
            '陪同地址',
            '创建订单时间',
            '支付状态',
            '支付时间',
            '服务状态',
            '服务完成时间'
        ];

        $param = Request::param("");
        $where = $this->search($param);
        $arr=OrderModel::getSelect($where);

        if(!count($arr)){
            $this->error("没有数据！");
            return false;
        }

        $data=[];
        foreach ($arr as $key=>$info){
            $data[$key]['ID']=$info['id'];
            $data[$key]['mobile']=$info['user']['mobile'];
            $data[$key]['order_number']=$info['order_number']." ";
            $data[$key]['service_name']=$info['service']['title'];
            $data[$key]['brand_name']=empty($info['brand_name'])?'':$info['brand_name'];
            $data[$key]['series_name']=empty($info['series_name'])?'':$info['series_name'];
            $data[$key]['car_name']=empty($info['car_name'])?'':$info['car_name'];
            $data[$key]['real_name']=$info['real_name'];
            $data[$key]['real_mobile']=$info['mobile'];
            $data[$key]['go_time']=date('Y-m-d H:i:s',$info['go_time']);
            $data[$key]['address']=$info['address'];
            $data[$key]['create_time']=$info['create_time'];
            $data[$key]['status']=arrText($info['status'],[0=>'未支付',1=>'已支付']);
            $data[$key]['pay_time']=empty($info['pay_time'])?'':date('Y-m-d H:i:s',$info['pay_time']);
            $data[$key]['order_type']=arrText($info['order_type'],[0=>'未服务',1=>'已服务']);
            $data[$key]['finish_time']=empty($info['finish_time'])?'':date('Y-m-d H:i:s',$info['finish_time']);
        }
        exportExcel($name,$tit,$data);
    }

}